import { createRouter, createWebHistory } from 'vue-router'

// 路由配置
const routes = [
  // 登录和用户相关路由
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/Login.vue'),
    meta: { 
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/auth/Profile.vue'),
    meta: { 
      title: '个人信息',
      requiresAuth: true
    }
  },
  
  // 图书管理路由
  {
    path: '/book',
    name: 'BookList',
    component: () => import('@/views/book/List.vue'),
    meta: {
      title: '图书管理',
      requiresAuth: true
    }
  },
  {
    path: '/book/detail/:id',
    name: 'BookDetail',
    component: () => import('@/views/book/Detail.vue'),
    meta: {
      title: '图书详情',
      requiresAuth: true
    }
  },
  {
    path: '/book/add',
    name: 'BookAdd',
    component: () => import('@/views/book/Edit.vue'),
    meta: {
      title: '添加图书',
      requiresAuth: true,
      requiresAdmin: true
    }
  },
  {
    path: '/book/edit/:id',
    name: 'BookEdit',
    component: () => import('@/views/book/Edit.vue'),
    meta: {
      title: '编辑图书',
      requiresAuth: true,
      requiresAdmin: true
    }
  },
  
  // 用户管理路由
  {
    path: '/user',
    name: 'UserList',
    component: () => import('@/views/user/List.vue'),
    meta: {
      title: '用户管理',
      requiresAuth: true,
      requiresAdmin: true
    }
  },
  
  // 会话管理路由
  {
    path: '/session/online',
    name: 'OnlineUsers',
    component: () => import('@/views/session/Online.vue'),
    meta: {
      title: '在线用户',
      requiresAuth: true,
      requiresAdmin: true
    }
  },
  
  // 日志管理路由
  {
    path: '/log/login',
    name: 'LoginLog',
    component: () => import('@/views/log/LoginLog.vue'),
    meta: {
      title: '登录日志',
      requiresAuth: true,
      requiresAdmin: true,
      keepAlive: false
    }
  },
  {
    path: '/log/operation',
    name: 'OperationLog',
    component: () => import('@/views/log/OperationLog.vue'),
    meta: {
      title: '操作日志',
      requiresAuth: true,
      requiresAdmin: true,
      keepAlive: false
    }
  },
  
  // 首页重定向
  {
    path: '/',
    redirect: '/book'  // 将首页重定向到图书列表
  },
  
  // 错误页面
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/error/403.vue'),
    meta: { title: '403', requiresAuth: false }
  },
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    meta: { title: '404', requiresAuth: false }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 图书管理系统` : '图书管理系统'
  
  console.log(`路由导航: 从 ${from.path} 到 ${to.path}`)
  
  // 从localStorage获取token和用户信息
  const token = localStorage.getItem('token')
  const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
  const userRole = userInfo.role || ''
  
  console.log('当前token状态:', token ? '已存在' : '不存在', '角色:', userRole)
  
  // 检查路由是否需要管理员权限
  const requiresAdmin = to.meta.requiresAdmin
  
  // 路由需要认证
  if (to.meta.requiresAuth) {
    if (!token) {
      // 无token，重定向到登录页
      console.log('无token，重定向到登录页')
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else if (requiresAdmin && userRole !== 'admin') {
      // 需要管理员权限但用户不是管理员
      console.log('需要管理员权限，重定向到403页面')
      next({ path: '/403' })
    } else {
      // 有token且权限满足
      console.log('权限验证通过，允许访问:', to.path)
      next()
    }
  } else {
    // 如果是登录页面且已登录，则重定向到首页
    if (to.path === '/login' && token) {
      console.log('已登录用户访问登录页，重定向到首页')
      next({ path: '/' })
    } else {
      console.log('允许访问不需要认证的页面:', to.path)
      next()
    }
  }
})

export default router 